package com.googlecode.alogwe.sprockit.js;

import java.lang.reflect.InvocationTargetException;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class JSSystem extends ScriptableObject {

	/**
	 * The zero-parameter constructor, required by Rhino for all host objects
	 * <p>
	 * When Context.defineClass is called with this class, it will construct
	 * System.prototype using this constructor.
	 */
	public JSSystem() {
	}

	/**
	 * The Java method defining the JavaScript constructor.
	 */
	public void jsConstructor() {
	}

	private static final String	JS_CLASSNAME	= "System";

	/**
	 * Returns the name of this JavaScript class. Typically the same name as the
	 * constructor.
	 */
	@Override
	public String getClassName() {
		return JS_CLASSNAME;
	}

	/**
	 * Creates it's own JavaScript host object in the given context/scope
	 * <p>
	 * <b>TODO</b> Figure out a better way to do this.
	 * 
	 * @param cx
	 *            The Context used to create the JavaScript host object
	 * @param scope
	 *            The object in the JavaScript environment that will receive
	 *            this newly created host object
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws InvocationTargetException
	 */
	public static void createJsHostObject(Context cx, Scriptable scope) throws IllegalAccessException,
			InstantiationException, InvocationTargetException {
		ScriptableObject.defineClass(scope, JSSystem.class);
		Object[] args = {};
		Scriptable system = cx.newObject(scope, JS_CLASSNAME, args);
		scope.put(JS_CLASSNAME, scope, system);
	}

}